<template>
	<view class="code_view">
		<image src="@/static/home/code.png"></image>
	</view>
	<view class="tips">扫描识别内容</view>
	<view class="btn" @click="submit">
		<uv-icon name="scan" color="#FFFFFF" size="30"></uv-icon>
		<text>开始扫描</text>
	</view>
</template>

<script setup>
	const baseApiUrl = import.meta.env.VITE_BASE_URL;
	import {
		onShow,
		onLoad
	} from "@dcloudio/uni-app";
	import {
		ref,
		onMounted
	} from "vue"
	import {
		storeToRefs
	} from 'pinia'
	import useBase from "@/hooks/useBase.js";
	import useStore from "@/store/index.js";
	import {
		indexOcr,
		upload
	} from '@/util/request/api.js';

	const {
		useLoginStore
	} = useStore();

	const {
		token
	} = storeToRefs(useLoginStore);
	const {
		router
	} = useBase();

	const submit = () => {

		uni.chooseMedia({
			count: 1,
			mediaType: ['image'],
			success: async (res) => {
				let filePath = res.tempFiles[0].tempFilePath;
				await uni.uploadFile({
					url: `${baseApiUrl}/api/common/upload`,
					filePath: filePath,
					name: 'file',
					header: {
						token: token.value
					},
					success: async (data) => {
						data = JSON.parse(data.data);
						let fullurl = data.data.fullurl;
						// console.log(fullurl)
						uni.setStorageSync('ocr_url', fullurl);
						router('/pagesA/home/orderEdit');
					}
				})
			}
		})
	};
	onLoad(() => {
		if (token.value) return;
		uni.$uv.toast("请登录后操作")
		setTimeout(() => {
			uni.redirectTo({
				url: '/pages/login/login'
			})
		}, 1000);
	});
	onShow(() => uni.removeStorageSync('ocr_url'));
</script>

<style lang="scss" scoped>
	.code_view {
		width: 100%;
		border-radius: 40rpx;
		padding: 120rpx 0 60rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			width: 430rpx;
			height: 430rpx;
		}
	}

	.tips {
		text-align: center;
		line-height: 36rpx;
		color: #1C2023;
		font-size: 36rpx;
		font-weight: 600;
	}

	.btn {
		width: 630rpx;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		background: #2282FC;
		box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(0, 92, 255, 0.3);
		border-radius: 15rpx;
		margin: 120rpx auto 0;
		display: flex;
		justify-content: center;

		text {
			margin-left: 15rpx;
			color: #FFFFFF;
			font-size: 28rpx;
			font-weight: 600;
		}
	}
</style>